﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGPeWJNM.BaseClasses;

namespace TimeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string instancePath, timereportPath;
            readArgs(args, out instancePath, out timereportPath);

            var iDataset = new LGPeWJNM.DataAccessLayer.InstanciaDataset(instancePath);
            var times = new List<TimeResults>();
            foreach (var instanceKey in iDataset.Keys)
            {
                var res = new TimeResults();
                var instance = iDataset[instanceKey];
                DateTime startTime, endTime;
                //var McG = new LGPeWJNM.TeitzComGillet.McG();
                var McG = new LGPeWJNM.TeitzEBart.TeitzEBart();
                var gj = new LGPeWJNM.GilletJohnson.GilletJohnson();
                var Vmp = new LGPeWJNM.Ime.Ime();
                var _2Opt = new LGPeWJNM._2Opt._2Opt();
                var InstanceResults = new List<double>();

                startTime = DateTime.Now;
                for (int i = 0; i < 2000; i++)
                {
                    try
                    {
                        var meds = McG.Processar(instance.Destinos, instance.QuantidadeVeiculos, instance.CapacidadeVeiculo);
                        var o1 = gj.Processar(meds, instance.Destinos, instance.Deposito, instance.CapacidadeVeiculo);
                        var o2 = Vmp.Processar(o1, instance.Deposito, instance.QuantidadeVeiculos, instance.CapacidadeVeiculo);
                        var o3 = _2Opt.Processar(o2, instance.Deposito, instance.CapacidadeVeiculo, instance.QuantidadeVeiculos);
                        InstanceResults.Add(o3.CustoTotal());
                    }
                    catch { }
                }
                endTime = DateTime.Now;

                res.IdInstancia = instanceKey;
                res.Time = endTime.Subtract(startTime).TotalSeconds;
                res.Results = InstanceResults;
                times.Add(res);
            }
            using (var xw = System.Xml.XmlWriter.Create(timereportPath))
            {
                var xs = new System.Xml.Serialization.XmlSerializer(times.GetType());
                xs.Serialize(xw, times);
            }
        }

        private static void readArgs(string[] args, out string instance, out string timereport)
        {
            instance = timereport = null;
            if (args.Count() % 2 != 0)
                throw new ArgumentException("argumentos faltando");
            for (int i = 0; i < args.Count(); i++)
            {
                if (args[i].StartsWith("-"))
                    switch (args[i].Remove(0, 1))
                    {
                        case "instance": instance = args[i + 1];
                            continue;
                        case "timereport": timereport = args[i + 1];
                            continue;
                    }
            }
        }
    }
}
